#include "stdafx.h"
#include "IBasicSkinRenderImage.h"
#include "BasicSkinCanvas.h"

namespace BasicSkinView
{
	IBasicSkinRenderImage::IBasicSkinRenderImage()
	:m_pImage(NULL)
	{
		m_nCount = 1;
		m_nCurIndex = 0;
		m_eRenderStyle = ERENDERIMAGE_IMAGE;
		m_eAlignment =  (ERENDERIMAGE_ALIGNMENT)(ERENDERIMAGE_ALIGN_HORZCENTER|ERENDERIMAGE_ALIGN_VERTCENTER);
	}
	IBasicSkinRenderImage::~IBasicSkinRenderImage()
	{
		SetImage(NULL);
	}

	IBasicSkinRenderImage* IBasicSkinRenderImage::CreateRenderImage(ERENDERIMAGE_STYLE eStyle, 
			UINT uAli,Image* pIm)
	{
		IBasicSkinRenderImage* pRender = new IBasicSkinRenderImage();
		
		pRender->SetImage(pIm);
		pRender->SetAlignment(uAli);
		pRender->SetRenderStyle(eStyle);

		return pRender;
	}
	void IBasicSkinRenderImage::SetImage(Image* pImg)
	{
		if (m_pImage)
			delete m_pImage;
		m_pImage = pImg;
	}
	void IBasicSkinRenderImage::SetCurIndex(int nIndex)
	{
		m_nCurIndex = nIndex;
	}
	void IBasicSkinRenderImage::SetFrameCount(int nCount)
	{
		if (nCount <= 0)
			nCount = 1;
		m_nCount = nCount;
	}
	void IBasicSkinRenderImage::SetRenderStyle(ERENDERIMAGE_STYLE eStyle)
	{
		m_eRenderStyle = eStyle;
	}
	void IBasicSkinRenderImage::SetAlignment(UINT uAlignment)
	{
		m_eAlignment = (ERENDERIMAGE_ALIGNMENT)uAlignment;
	}

	void IBasicSkinRenderImage::OnRenderObject(CBasicSkinCanvas* pCanvas,RECT* prc)
	{
		switch(m_eRenderStyle)
		{
		case ERENDERIMAGE_IMAGE:
			RenderImage(pCanvas,prc);
			break;
		case ERENDERIMAGE_HORZBAR:
			RenderHorzBar(pCanvas,prc);
			break;
		}
	}
	void IBasicSkinRenderImage::RenderHorzBar(CBasicSkinCanvas* pCanvas,RECT* prc)
	{
		if (m_pImage)
		{
			int icy = m_pImage->GetHeight();
			int cy = prc->bottom - prc->top;

			int x = prc->left;
			int y = prc->top;
			int cx = prc->right - x;

			if (m_eAlignment&ERENDERIMAGE_ALIGN_VERTCENTER)
				y = (y + cy-icy)/2;
			else if (m_eAlignment&ERENDERIMAGE_ALIGN_VERTRIGHT)
				y = y + cy-icy;

			pCanvas->DrawTitleButton(m_pImage,x,y,cx,icy,m_nCurIndex,m_nCount,false);
		}
	}
	void IBasicSkinRenderImage::RenderImage(CBasicSkinCanvas* pCanvas,RECT* prc)
	{
		if (m_pImage == NULL || m_nCount == 0)
			return;
		int icx = m_pImage->GetWidth()/m_nCount;
		int icy = m_pImage->GetHeight();
		int x = prc->left;
		int y = prc->top;
		int cx = prc->right - x;
		int cy = prc->bottom - y;
		
		if (m_eAlignment&ERENDERIMAGE_ALIGN_HORZCENTER)
			x = x+(cx-icx)/2;
		else if (m_eAlignment&ERENDERIMAGE_ALIGN_HORZRIGHT)
			x = x + cx-icx;
		
		if (m_eAlignment&ERENDERIMAGE_ALIGN_VERTCENTER)
			y = y + (cy-icy)/2;
		else if (m_eAlignment&ERENDERIMAGE_ALIGN_VERTRIGHT)
			y = y + cy-icy;

		pCanvas->DrawStatusImage(m_pImage,x,y,m_nCurIndex,m_nCount);
	}

}